using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using LeisureCenter.Api;

namespace LeisureCenter.Controls
{
    public class PlaylistListBox : ListBox
    {
        private int playingIndex;

        public int PlayingIndex
        {
            get { return playingIndex; }
            set 
            {
                int lastPlayingIndex = playingIndex;
                playingIndex = Math.Min(value, Items.Count - 1);
                if (!Focused || lastPlayingIndex == SelectedIndex)
                    SelectedIndex = playingIndex;
                Invalidate();
            }
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            // TODO
            if (e.Index == playingIndex && e.Index != SelectedIndex)
                base.OnDrawItem(new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, e.State | DrawItemState.Selected | ~DrawItemState.Focus));
            else
            if (e.Index == playingIndex)
                base.OnDrawItem(new DrawItemEventArgs(e.Graphics, e.Font, e.Bounds, e.Index, e.State | DrawItemState.Selected));
            else
                base.OnDrawItem(e);
        }

        protected override void DrawItemText(ListBoxItem item, Graphics graphics, Rectangle textBounds, Brush textBrush)
        {
            // text format
            //StringFormat format = new StringFormat();
            //format.Alignment = StringAlignment.Near;// (graphics.MeasureString(item.Text, Font).Width > textBounds.Width) ? StringAlignment.Far : StringAlignment.Near;
            //format.LineAlignment = StringAlignment.Near;
            //format.FormatFlags |= StringFormatFlags.NoWrap;
            //
            // text
            //graphics.DrawString(item.Text, Font, textBrush, textBounds, format);

            // using textrenderer
            graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            TextFormatFlags flags = TextRenderer.MeasureText(graphics, item.Text, Font).Width > textBounds.Width ? TextFormatFlags.Right : TextFormatFlags.Default;
            TextRenderer.DrawText(graphics, item.Text, Font, textBounds, ((SolidBrush)textBrush).Color, flags);
        }

        protected override void OnLeave(EventArgs e)
        {
            SelectedIndex = playingIndex;
            base.OnLeave(e);
        }

        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            base.OnSelectedIndexChanged(e);
            if (SelectedIndex != playingIndex && (playingIndex > -1))
            {
                Rectangle rect = GetItemRectangle(playingIndex);
                if (rect.Bottom < ClientSize.Height)
                    Invalidate(rect);
            }
        }
    }

    public class PlaylistListBoxItem : ListBoxItem
    {
        public PlaylistListBoxItem(ListBox listBox, IPlaylistItem playlistItem)
            : base(listBox, playlistItem.Text, "default")
        {
        }

        public override string IconName
        {
            get
            {
                if (((PlaylistListBox)listBox).PlayingIndex == ((PlaylistListBox)listBox).Items.IndexOf(this))
                    return @"playing0";
                else
                    return base.IconName;
            }
            set
            {
                base.IconName = value;
            }
        }
    }
}
